{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c1478b21",
   "metadata": {},
   "source": [
    "# 生成单比特 待重构算符 和 生成随机初、末状态数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bd259716",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "初态数据维度为： (5000, 2)\n",
      "算符维度为 (2, 2)\n",
      "\n",
      "末态数据维度为： (5000, 2)\n",
      "导出完成啦\n",
      "\n",
      "拆分后的数据维度分别为：\n",
      "train_x: (4000, 2) eval_x: (1000, 2) train_y: (4000, 2) eval_y: (1000, 2)\n"
     ]
    }
   ],
   "source": [
    "from mindquantum import *\n",
    "import numpy as np\n",
    "np.random.seed(1)\n",
    "\n",
    "qubit_num = 1 # 待重构算符的比特数\n",
    "data_num = 5000 # 用于训练的样本总数\n",
    "u_num = 1 # 要构建的算符数\n",
    "\n",
    "# 搭建用于产生量子初态的线路\n",
    "circ = U3('theta0','theta1','theta2', 0)\n",
    "params_name = circ.params_name\n",
    "\n",
    "# 生成随机初态数据\n",
    "state_list = []\n",
    "for i in range(data_num):\n",
    "    params = 2*np.pi*np.random.rand(len(params_name))\n",
    "    state = circ.get_qs(pr=dict(zip(params_name, params)), ket=False)\n",
    "    state_list.append(state)\n",
    "        \n",
    "init_states = np.array(state_list)\n",
    "print('\\n初态数据维度为：', init_states.shape)\n",
    "\n",
    "# 搭建作为待重构算符的线路\n",
    "\n",
    "u_mat = X.matrix()\n",
    "\n",
    "print('算符维度为', u_mat.shape)\n",
    "\n",
    "# 根据初态，对每个幺正算符产生末态\n",
    "state_list = [u_mat@init_state for init_state in init_states]\n",
    "    \n",
    "final_states = np.array(state_list)\n",
    "print('\\n末态数据维度为：', final_states.shape)\n",
    "\n",
    "# 拆分数据集\n",
    "train_x = init_states[:4000] # 按 7:3 分割\n",
    "eval_x = init_states[4000:]\n",
    "train_y = final_states[:4000]\n",
    "eval_y = final_states[4000:]\n",
    "\n",
    "# 分别保存 新训练集 和 新验证集\n",
    "np.save('./src/1_qubit_x_train_x.npy', train_x)\n",
    "np.save('./src/1_qubit_x_eval_x.npy', eval_x)\n",
    "np.save('./src/1_qubit_x_train_y.npy', train_y)\n",
    "np.save('./src/1_qubit_x_eval_y.npy', eval_y)\n",
    "print('导出完成啦')\n",
    "\n",
    "# 再次导入，以验证是否完成任务\n",
    "a = np.load('./src/1_qubit_x_train_x.npy', allow_pickle=True)\n",
    "b = np.load('./src/1_qubit_x_eval_x.npy', allow_pickle=True)\n",
    "c = np.load('./src/1_qubit_x_train_y.npy', allow_pickle=True)\n",
    "d = np.load('./src/1_qubit_x_eval_y.npy', allow_pickle=True)\n",
    "\n",
    "print('\\n拆分后的数据维度分别为：')\n",
    "print('train_x:', a.shape, 'eval_x:', b.shape, 'train_y:', c.shape, 'eval_y:', d.shape)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
